{extend name="iframe" /}

{block name="body"}
<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">
    <form action="{:request()->url()}" method="post" class="xn_ajax2" data-type="open">
        <div class="layui-form-item">
            <label class="layui-form-label">行业类型</label>
            <div class="layui-input-inline">
                <select name="parent_typeid" lay-filter="parent_typeid" id="parent_typeid">
                    <option value="0">选择类型</option>
                    {foreach name="parent_typelist" item="vo"}
                    <option value="{$vo.id}" {if condition="$data.parent_typeid eq $vo.id"}selected{/if} >{$vo.title}</option>
                    {/foreach}
                </select>
            </div>
            <div class="layui-input-inline">
                <select name="typeid" id="typeid">
                    <option value="0">选择类型</option>
                    {foreach name="typelist" item="vo"}
                    <option value="{$vo.id}" {if condition="$data.typeid eq $vo.id"}selected{/if} >{$vo.title}</option>
                    {/foreach}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">项目名称</label>
            <div class="layui-input-block">
                <input type="text" name="title" id="project" lay-verify="required" placeholder="" autocomplete="off" class="layui-input" value="{$data.title}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">项目别称</label>
            <div class="layui-input-block">
                <input type="text" name="alias" lay-verify="required" placeholder="" autocomplete="off" class="layui-input" value="{$data.alias}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">公司名称</label>
            <div class="layui-input-block">
                <input type="text" name="company" placeholder="" autocomplete="off" class="layui-input" value="{$data.company}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">广告语</label>
            <div class="layui-input-block">
                <input type="text" name="slogan" placeholder="" autocomplete="off" class="layui-input" value="{$data.slogan}">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">项目摘要</label>
            <div class="layui-input-block">
                <textarea name="description" class="layui-textarea">{$data.description}</textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">加盟费(最低)</label>
            <div class="layui-input-block">
                <input type="text" name="cost_min" class="layui-input" value="{$data.cost_min}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">加盟费(最高)</label>
            <div class="layui-input-block">
                <input type="text" name="cost_max" class="layui-input" value="{$data.cost_max}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">品牌源地</label>
            <div class="layui-input-block">
                <input type="text" name="origin" placeholder="" autocomplete="off" class="layui-input" value="{$data.origin}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">主营类目</label>
            <div class="layui-input-block">
                <input type="text" name="business" placeholder="" autocomplete="off" class="layui-input" value="{$data.business}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">门店数量</label>
            <div class="layui-input-block">
                <input type="text" name="shop_num" placeholder="" autocomplete="off" class="layui-input" value="{$data.shop_num}">
            </div>
        </div>
        <!-- <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">项目介绍</label>
            <div class="layui-input-block">
                <textarea name="content" class="layui-textarea">{$data.content}</textarea>
            </div>
        </div> -->
        <div class="layui-form-item">
            <label class="layui-form-label">项目介绍</label>
            <div class="layui-input-block">
                <blockquote class="layui-elem-quote layui-quote-nm">
                    <div class="layui-form-item">
                        <button type="button" class="layui-btn" onclick="addDiv()">增加</button>
                    </div>
                    <div id="box">
                        {foreach name="contentlist" item="vo"}
                        <div class="layui-form-item layui-form-text">
                            <textarea class="layui-textarea" data-id="{$vo.id}">{$vo.content}</textarea>
                            <button type="button" class="layui-btn layui-btn-normal" onclick="editContent(this)">修改</button>
                            <button type="button" class="layui-btn layui-btn-danger" onclick="deleteContent(this)">删除</button>
                        </div>
                        {/foreach}
                    </div>
                </blockquote>
            </div>
        </div>
        
        
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" lay-submit class="layui-btn">保存</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
{/block}
{block name="js"}
<script>
    var form = layui.form;
    form.on('select(parent_typeid)', function(res) { 
        $("#typeid").empty();
        if(res.value != 0){
            $.get("{:url('admin/DataType/getSonType')}?pid="+res.value,function(result){
                if(result.status){
                    $("#typeid").append(new Option('请选择',0));
                    $.each(result.data,function(index,item){
                        $("#typeid").append(new Option(item.title,item.id));
                    });
                    form.render("select");
                }
            });
        }else{
            $("#typeid").append(new Option('请选择',0));
            form.render("select");
        }
    })

    function addDiv(){
        var html = getHtml();
        $('#box').append(html)
    }
    function getHtml(){
        var html = '<div class="layui-form-item layui-form-text">\
                        <textarea class="layui-textarea"></textarea>\
                        <button type="button" class="layui-btn layui-btn-normal" onclick="saveContent(this)">保存</button>\
                        <button type="button" class="layui-btn layui-btn-danger" onclick="removeDiv(this)">移除</button>\
                    </div>';
        return html;
    }
    function removeDiv(obj){
        $(obj).parent().remove();
    }

    function saveContent(obj){
        var content = $(obj).prev().val();
        if(content == ''){
            layer.msg('不能为空', {icon: 2,time: 1500});
            return;
        }
        var parent_typeid = $('#parent_typeid').val();
        var typeid = $('#typeid').val();
        var project = $('#project').val();
        $.post("{:url('DataProjectContent/add')}",{parent_typeid:parent_typeid,typeid:typeid,project:project,content:content},function(res){
            layer.msg(res.msg, {time: 1500});
            if(res.code){
                location.reload();
            }
        });
    }

    function editContent(obj){
        var id = $(obj).prev().attr('data-id');
        var content = $(obj).prev().val();
        if(id == '' || id == 0){
            layer.msg('参数错误', {icon: 2,time: 1500});
            return;
        }
        if(content == ''){
            layer.msg('不能为空', {icon: 2,time: 1500});
            return;
        }
        var parent_typeid = $('#parent_typeid').val();
        var typeid = $('#typeid').val();
        var project = $('#project').val();
        $.post("{:url('DataProjectContent/edit')}",{id:id,parent_typeid:parent_typeid,typeid:typeid,project:project,content:content},function(res){
            layer.msg(res.msg, {time: 1500});
        });
    }

    function deleteContent(obj){
        var id = $(obj).prev().prev().attr('data-id');
        if(id == '' || id == 0){
            layer.msg('参数错误', {icon: 2,time: 1500});
            return;
        }
        $.get("{:url('DataProjectContent/delete')}",{id:id},function(res){
            layer.msg(res.msg, {time: 1500});
            if(res.code){
                $(obj).parent().remove();
            }
        });
    }
</script>
{/block}